<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/12/19
 * Time: 11:28
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Gz extends Base
{
    /**
     * 故障列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $where['gz_status'] = 1;
            $gz_name = input('name'); // 故障名称

            // 关键字
            if(isset($gz_name) && $gz_name != ''){
                $where['gz_name'] = ['like', '%' . $gz_name . '%'];
            }

            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('gz')->where($where)->count();// 获取总条数
            $lists = Db::name('gz')
                ->where($where)
                ->page($NowPage, $limits)
                ->order('gz_status asc, gz_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 新增故障
     * @return mixed|\think\response\Json
     */
    public function gzAdd(){
        if(Request::instance()->isPost()){
            $gz_name = input('gz_name');

            $insert = [
                'gz_name' => $gz_name,
                'gz_time' => createTime()
            ];
            $res = Db::table('up_gz')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        return $this->fetch();
    }

    /**
     * 更新故障
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function gzUpdate(){
        $gz_id = input('gz_id');
        if(Request::instance()->isPost()){
            $gz_name = input('gz_name');

            $update = [
                'gz_id' => $gz_id,
                'gz_name' => $gz_name,
                'gz_update_time' => createTime()
            ];
            $res = Db::table('up_gz')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $gz = Db::table('up_gz')->where('gz_id', $gz_id)->field('gz_name')->find();
        return $this->fetch('', ['gz_id' => $gz_id, 'gz' => $gz]);
    }

//    /**
//     * 上架/下架故障
//     * @return \think\response\Json
//     */
//    public function gzStatus(){
//        $gz_id = input('id');
//        $status = Db::table('up_gz')->where('gz_id', $gz_id)->value('gz_status');
//        $newStatus = $status == 1 ? 2 : 1;
//
//        $res = Db::table('up_gz')->where('gz_id', $gz_id)->setField('gz_status', $newStatus);
//        if($res){
//            return json(['code' => 200, 'msg' => '操作成功']);
//        }else{
//            return json(['code' => 100, 'msg' => '操作失败']);
//        }
//    }

    /**
     * 删除故障
     * @return \think\response\Json
     */
    public function gzDel(){
        $gz_id = input('id');

        $res = Db::table('up_gz')->where('gz_id', $gz_id)->setField('gz_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}